
:: First time run this script to get all projects
@echo off

echo TODO: check the proxy still working....
REM echo "Restart proxy server"
REM net stop Cntlm
REM net start Cntlm

set CUR_DIR=%~dp0
echo %CUR_DIR%
cd %CUR_DIR%
REM use windows credentials manager, users need to enter credentials on the first time of running this script
git config --global credential.helper wincred

for /f %%P in (projects_deploy.txt) do (
  if not "%%P"=="" call:sync-project %%P
)

:sync-project - passing a project name
@echo off
SETLOCAL


if "%~1"=="" goto:EOF

set PROJECT_NAME=%~1
set DIR_PATH=%PROJECT_NAME%.git
set PROXY_SERVER=http://127.0.0.1:3128
set GIT_PATH="%GIT_HOST%/%PROJECT_NAME%.git"

REM Prepare the work dir
set BASE_DIR=projects.git
if not exist %BASE_DIR% (
  md %BASE_DIR%
)
cd %BASE_DIR%

if not exist %DIR_PATH% (
  echo "Init project from %GIT_PATH%"
  git clone --mirror ^
    -c http.sslVerify=false ^
    -c push.default=matching ^
    -c http.http://git.oschina.net.proxy=%PROXY_SERVER% ^
    "http://git.oschina.net/diannaowa/bgit-deploy.git" %DIR_PATH%
)
echo #### %PROJECT_NAME%****
cd %DIR_PATH%
git fetch
PAUSE
if %ERRORLEVEL% EQU 0 (
  git push --mirror http://10.60.77.170:8009/gogs/bgit-deploy.git
) else (
  echo 'git fetch failed please check proxy.'
)
ENDLOCAL
goto:EOF


PAUSE